<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: GetUserDetail.php
 * Date: 2017/2/11
 * Time: 21:50
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;


class GetUserDetail extends PageBase
{
    private $objUser;

    public function __construct()
    {
        parent::__construct();
        $this->objUser = new UserAccount();
    }

    public function excute($param)
    {
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        $userInfo = $this->objUser->getUserAccountByUserids($param['user_id']);
        //var_dump($userInfo);
        if (false === $userInfo) {
            return $this->getRes(Error::ERR_SYS);
        }
        if (!count($userInfo)) {
            return $this->getRes(Error::ERR_USER_NOT_EXIST);
        }
        $userInfo = $userInfo[0]->toArray();
        if (!empty($userInfo['extend_info'])) {
            $extend_info = json_decode($userInfo['extend_info'], true);
            $userInfo = array_merge($userInfo, $extend_info);
        }
        $this->data = $userInfo;
        return $this->getRes();

    }


    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
        ];
        $msg = [
            'user_id.require' => 'user_id必须',
        ];
        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function zeroCount($user_id, $type)
    {
        return [
            'user_id' => (int)$user_id,
            'currency_type' => (int)$type,
            'amount' => 0
        ];
    }
}
